package com.example.facedetectionopencv;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button image_detection = (Button)findViewById(R.id.image_detection);
		image_detection.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(intent, 8888);
			}
		});
		Button camera_detection = (Button)findViewById(R.id.camera_detection);
		camera_detection.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent().setClass(getBaseContext(), CameraDetectionActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == 8888 && resultCode == RESULT_OK && data != null)
		{
			Uri imageUri = data.getData();
			if (imageUri != null)
			{
				Cursor c = getContentResolver().query(
						imageUri,
						new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
						null,
						null,
						null);
				c.moveToFirst();
				String imagePath = c.getString(0);
				c.close();
				Intent intent = new Intent().setClass(getApplicationContext(), ImageDetectionActivity.class);
				intent.putExtra("ImagePath", imagePath);
				startActivity(intent);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}